package com.boolib.load.mail.impl;


import com.boolib.commons.Const;
import com.boolib.commons.entity.ServerResponse;
import com.boolib.load.mail.IMailService;
import com.boolib.load.mail.MailHelper;
import com.boolib.commons.util.CacheUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;


import java.util.Random;
import java.util.concurrent.ExecutionException;

@Service
public class MailServiceImpl implements IMailService {





    @Autowired
    MailHelper mailHelper;

    @Autowired
    private TemplateEngine templateEngine;




    @Override
    public void sendForgetMail(String form , String to, String title, Integer id){


        String code = code4() ;


        CacheUtil.getInstance().put(getToken(id,code),id);


        Context context = new Context();
        context.setVariable("code",code);


        String content = templateEngine.process("forgetPass",context);
        mailHelper.sendHtmlMail(form,to,title,content);

    }


    @Override
    public void sendSysUserForgetMail(String form, String to, String title, Integer id){


        String code = code4() ;


        CacheUtil.getInstance().put("sys_"+getToken(id,code),id);


        Context context = new Context();
        context.setVariable("code",code);


        String content = templateEngine.process("forgetPass",context);
        mailHelper.sendHtmlMail(form,to,title,content);

    }






    @Override
    public ServerResponse checkMailToken(Integer id, String code){

        Integer var1 = null;
        try {
            var1 = CacheUtil.getInstance().getValue(getToken(id,code));
        } catch (ExecutionException e) {
            e.printStackTrace();
        }


        if(var1==-1){
            return ServerResponse.createByError(Const.User.Status.TOKENEXPIRE.getCode(),Const.User.Status.TOKENEXPIRE.getMessage());
        }

        if(!var1.equals(id)){
            return ServerResponse.createByError(Const.User.Status.TOKENERROR.getCode(),Const.User.Status.TOKENERROR.getMessage());
        }


        return ServerResponse.createBySuccess();

    }



    @Override
    public ServerResponse checkSysUserMailToken(Integer id, String code){

        Integer var1 = null;
        try {
            var1 = CacheUtil.getInstance().getValue("sys_"+getToken(id,code));
        } catch (ExecutionException e) {
            e.printStackTrace();
        }


        if(var1==-1){
            return ServerResponse.createByError(Const.User.Status.TOKENEXPIRE.getCode(),Const.User.Status.TOKENEXPIRE.getMessage());
        }

        if(!var1.equals(id)){
            return ServerResponse.createByError(Const.User.Status.TOKENERROR.getCode(),Const.User.Status.TOKENERROR.getMessage());
        }


        return ServerResponse.createBySuccess();

    }









    private String code4(){
        String str="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        StringBuilder sb=new StringBuilder(4);
        for(int i=0;i<4;i++)
        {
            //public int nextInt(int n) 该方法的作用是生成一个随机的int值，该值介于[0,n)的区间，也就是0到n之间的随机int值，包含0而不包含n。
            char ch=str.charAt(new Random().nextInt(str.length()));
            sb.append(ch);
        }
        return  sb.toString();

    }


    private String getToken(Integer id ,String code)
    {
       return  id+"_"+code;

    }
}
